package com.lanhai.zhonglai.dispatch;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lanhai.zhonglai.MsgCore;
import com.lanhai.zhonglai.beans.AgvBean;
import com.lanhai.zhonglai.beans.AgvMountBean;
import com.lanhai.zhonglai.beans.DotBean;
import com.lanhai.zhonglai.beans.StationBean;
import com.lanhai.zhonglai.dao.RoadDao;
import com.lanhai.zhonglai.dao.StationGroupDao;
import com.lanhai.zhonglai.enums.AgvMountTypeEnum; 
import com.lanhai.zhonglai.enums.TransDirectionEnum;
import com.lanhai.zhonglai.interfaces.IAgv;
import com.lanhai.zhonglai.interfaces.IAgvEvent;
import com.lanhai.zhonglai.interfaces.IDot; 
import com.lanhai.zhonglai.interfaces.IRoad;
import com.lanhai.zhonglai.interfaces.ISendData;
import com.lanhai.zhonglai.interfaces.IStation;
import com.lanhai.zhonglai.interfaces.IStationMount;
import com.lanhai.zhonglai.interfaces.IStationProtocol;

/**
 * 离开缓存机的处理
 * 
 * @author zhengwei
 *
 */
@Component
public class CacheLeaveCheck {
	@Autowired
	IAgv iAgv;

	@Autowired
	IStation iStation;

	@Autowired
	ISendData sendTools;

	@Autowired
	IAgvEvent iAgvEvent;

	@Autowired
	IStationMount iStationMount;

	@Autowired
	IRoad iRoad;

	@Autowired
	IDot iDot;

	public void check(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent,
			AgvMountBean amount) {

		int dotno = dot.getDotno();
		switch (dotno) {
		case 252:
			leaveZRS(dot, agv, station, stationEvent, amount);
			break;
		case 254:
			leaveZRX(dot, agv, station, stationEvent, amount);
			break;
		case 256:
			leaveKS(dot, agv, station, stationEvent, amount);
			break;
		case 258:
			leaveJP(dot, agv, station, stationEvent, amount);
			break;
		case 260:
			leaveBSG(dot, agv, station, stationEvent, amount);
			break;
		case 262:
			leaveTH(dot, agv, station, stationEvent, amount);
			break;
		case 264:
			leavePOPAID(dot, agv, station, stationEvent, amount);
			break;
		case 266:
			leaveBOE(dot, agv, station, stationEvent, amount);
			break;
		case 268:
			 
			break;
		case 270:
			leaveSW(dot, agv, station, stationEvent, amount);
			break;
		case 272:
			leaveBM(dot, agv, station, stationEvent, amount);
			break;
		case 274:
			leaveBM(dot, agv, station, stationEvent, amount);
			break;
		case 276:
			leaveZMS(dot, agv, station, stationEvent, amount);
			break;
		case 278:
			leaveZMX(dot, agv, station, stationEvent, amount);
			break;
		default:
			break;
		}
	}
	
	
	void toQR(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent,String nextProcess,int road,boolean fromHC) {

		
		//离开缓存机
		iStation.unlockStationForDot(dot.getDotno(), agv.getAgvId());
		
		// 保存小车状态
		agv.setTransing(false);
		agv.setFinishTrans(true);
		agv.setUpHasTrans(false);
		agv.setDownHasTrans(false);  
		agv.setNexttStationName("");
		agv.setNextProcess(nextProcess);
		agv.setSpecialline(0);
		agv.setNextRoad(road);
		agv.setToHC(false);
		agv.setFromHC(fromHC);
		iAgv.saveCache(agv);

		MsgCore.sendMsg(agv.getAgvId(), "寻找下一道工序");
		  
		// 清空机台装料情况（不是解锁机台）
		iStationMount.clear(dot.getDotno()); 
		// 让小车完成传输，退回二维码处
		iAgvEvent.finishTrans(agv);

		// 向机台发送离开的信号 
		byte[] cmd = stationEvent.agvLeave(station.getLine(), station.getFillType());
		sendTools.sendHCData(station.getIp(), station.getPort(), station.getLine(), "AGV离开", cmd,
				station.getStationname());
	}

	/**
	 * 制绒上料，直接离开机台即可
	 * @param dot
	 * @param agv
	 * @param station
	 * @param stationEvent
	 * @param amount
	 */
	void leaveZRS(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
		if(amount.getMountType()==AgvMountTypeEnum.AllEmpty) {
			//空车
			int road=iRoad.getNormalRoad(dot.getDotno()-1, 362);
			if(road>0) {
				toQR(dot, agv, station, stationEvent, "", road,false);
			}else {
				MsgCore.sendMsg(agv.getAgvId(), "缺少路线");
			}
		}else {
			//带花篮的
			String process="制绒上料";
			StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), process,0,true );
			if(gbean!=null) {
				RoadDao road = iRoad.getTransRoad(dot.getDotno()-1, gbean.getDotno());
				if (road != null && road.getId() > 0) {
					//有路才锁定 
					toQR(dot,agv , station,stationEvent, process, road.getId(),true);
				} else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", dot.getDotno()-1, gbean.getDotno())); 
				}  
			}
		}
		
	}

	/**
	 * 制绒下料
	 * @param dot
	 * @param agv
	 * @param station
	 * @param stationEvent
	 * @param amount
	 */
	void leaveZRX(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			// 空车

			// 找扩散下料
			String process = "扩散下料";
			// 算28的待遇
			StationGroupDao gbean = iStation.fetchNextOutStation(dot.getDotno(), agv.getAgvId(), process, true ); 
			if (gbean != null) {
				RoadDao road = iRoad.getTransRoad(406, gbean.getDotno());
				if (road != null && road.getId() > 0) {
					// 有路才锁定 
					toQR(dot, agv, station, stationEvent, process, road.getId(),false);
				} else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", 406, gbean.getDotno())); 
				}
			} else {
				// 空车直接去425
				toQR(dot, agv, station, stationEvent, "", 0,false);
			}

		} else {
			//带料的车
			String process = "扩散上料";
			StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), process,0,true );
			if (gbean != null) {
				RoadDao road = iRoad.getTransRoad(406, gbean.getDotno());
				if (road != null && road.getId() > 0) {
					// 有路才锁定 
					toQR(dot, agv, station, stationEvent, process, road.getId(),true);
				} else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", 406, gbean.getDotno())); 
				}
			} else {
				// 不动
			}
		}
	}

	/**
	 * 扩散缓存机
	 * @param dot
	 * @param agv
	 * @param station
	 * @param stationEvent
	 * @param amount
	 */
	void leaveKS(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
		//直接去425
		toQR(dot, agv, station, stationEvent, "", 0,false); 
	}

	/**
	 * 碱抛下料
	 * @param dot
	 * @param agv
	 * @param station
	 * @param stationEvent
	 * @param amount
	 */
	void leaveJP(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			//空车,直接去406 走256
			toQR(dot, agv, station, stationEvent, "", 256,false);  
		}else {
			//先看POPAID有没有上料，有就去  368
			String process="POPAID上料";
			StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), process,0,true );
			if (gbean != null) {
				int roadid=iRoad.getNormalRoad(dot.getDotno()-1, 368);
				if(roadid>0) {
					toQR(dot, agv, station, stationEvent, "POPAID上料", roadid,true); 
				}else {
					//缺少路线
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", dot.getDotno()-1, 368)); 
				}
			} else {
				// 不动
			} 
		}
	}

	/**
	 * 去碱抛上料（空花篮
	 * @param dot
	 * @param agv
	 * @param station
	 * @param stationEvent
	 * @param amount
	 */
	void leaveBSG(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			//空车,直接去406 走256
			toQR(dot, agv, station, stationEvent, "碱抛上料", 256,false);  
		}else {
			String process = "碱抛上料";
			StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), process,0,true );
			if (gbean != null) {
				RoadDao road = iRoad.getTransRoad(dot.getDotno()-1, gbean.getDotno());
				if (road != null && road.getId() > 0) {
					// 有路才锁定 
					toQR(dot, agv, station, stationEvent, process, road.getId(),true);
				} else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", dot.getDotno()-1, gbean.getDotno())); 
			    }
			} else {
				// 不动
			}
		}
	}

	/**
	 * 离开退火缓存机
	 * @param dot
	 * @param agv
	 * @param station
	 * @param stationEvent
	 * @param amount
	 */
	void leaveTH(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			//空车，直接去479
			int roadid= iRoad.getNormalRoad(dot.getDotno()-1, 479);
			if(roadid>0) {
				toQR(dot, agv, station, stationEvent, "", roadid,false);
			}else {
				MsgCore.sendMsg(agv.getAgvId(), "缺少路线");
			}
		}else {
			//带料的车，去392
			String process = "BOE上料";
			StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), process,0,true );
			if (gbean != null) { 
				int roadid= iRoad.getNormalRoad(dot.getDotno()-1, 392);
				if(roadid>0) {
					toQR(dot, agv, station, stationEvent,process, roadid,true);
				}else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", dot.getDotno()-1, gbean.getDotno())); 	
				}
			} else {
				// 不动
			}
		}
	}

	void leavePOPAID(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent,
			AgvMountBean amount) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			//空车 ,去392
			int roadid= iRoad.getNormalRoad(dot.getDotno()-1, 392);
			if(roadid>0) {
				//POPAID去前段
				toQR(dot, agv, station, stationEvent, "", roadid,false);
			}else {
				MsgCore.sendMsg(agv.getAgvId(), "缺少路线");
			} 
		}else {
			//有料的车，去479
			String process = "退火上料";
			StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), process,0,true );
			if (gbean != null) { 
				int roadid= iRoad.getNormalRoad(dot.getDotno()-1, 479);
				if(roadid>0) {
					toQR(dot, agv, station, stationEvent,process, roadid,true);
				}else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", dot.getDotno()-1, 479)); 
				}
			} else {
				// 不动
			}
		}
	}

	/**
	 * BOE缓存，
	 * @param dot
	 * @param agv
	 * @param station
	 * @param stationEvent
	 * @param amount
	 */
	void leaveBOE(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			//空车，去392 
			int roadid= iRoad.getNormalRoad(dot.getDotno()-1, 392);
			if(roadid>0) {
				toQR(dot, agv, station, stationEvent, "", roadid,false);
			}else {
				MsgCore.sendMsg(agv.getAgvId(), "缺少路线");
			} 
		}else {
			//有料，去451
			String process = "ALD湿法";
			StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), process,0,true );
			if (gbean != null) { 
				int roadid= iRoad.getNormalRoad(dot.getDotno()-1, 451);
				if(roadid>0) {
					toQR(dot, agv, station, stationEvent,process, roadid,true);
				}else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", dot.getDotno()-1, gbean.getDotno())); 
				}
			} else {
				// 不动
			}
		}
	}
	
	/**
	 * 离开丝网空花篮的缓存
	 * @param dot
	 * @param agv
	 * @param station
	 * @param stationEvent
	 * @param amount
	 */
	void leaveSW(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			//空车，去393 
			int roadid= iRoad.getNormalRoad(dot.getDotno()-1, 393);
			if(roadid>0) {
				toQR(dot, agv, station, stationEvent, "", roadid,false);
			}else {
				MsgCore.sendMsg(agv.getAgvId(), "缺少路线");
			} 
		}else {
			//有料，去451
			String process = "ALD干法";
			StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), process,0,true );
			if (gbean != null) { 
				int roadid= iRoad.getNormalRoad(dot.getDotno()-1, 477);
				if(roadid>0) {
					toQR(dot, agv, station, stationEvent,process, roadid,true);
				}else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", dot.getDotno()-1, 477)); 
				}
			} else {
				// 不动
			}
		}
	}

	/**
	 * 正膜上料（ALD下料） 276 这个点，离镀膜很近
	 * @param dot
	 * @param agv
	 * @param station
	 * @param stationEvent
	 * @param amount
	 */
	void leaveZMS(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			//空车
			List<String> processList=Arrays.asList("正膜下料","背膜下料"); 
			StationGroupDao gbean = iStation.fetchNextOutStation(dot.getDotno(), agv.getAgvId(), processList, true );
			 if(gbean!=null) { 
				RoadDao road = iRoad.getTransRoad(475, gbean.getDotno()); 
				if(road!=null && road.getId()>0) {
					toQR(dot, agv, station, stationEvent,gbean.getProcess(), road.getId(),false);
				}else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", 475, gbean.getDotno())); 
				}   
			}else {
				//返回空车,去476，走151
				toQR(dot, agv, station, stationEvent,"" , 151,false);
			} 
		}else {
			//带料，只能去正膜上料
			String process = "正膜上料" ; 
			StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), process,0,true );
			if(gbean!=null) { 
				RoadDao road = iRoad.getTransRoad(475, gbean.getDotno()); 
				if(road!=null && road.getId()>0) {
					toQR(dot, agv, station, stationEvent,process, road.getId(),true);
				}else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", 475, gbean.getDotno())); 	
				}  
			}else {
				//不动 
			} 
		}
	}

	/**
	 * 正膜下，只能考虑背膜 278这个点，离镀膜很近
	 * @param dot
	 * @param agv
	 * @param station
	 * @param stationEvent
	 * @param amount
	 */
	void leaveZMX(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			//空车，找正膜下料和背膜下料
			List<String> processList=Arrays.asList("正膜下料","背膜下料"); 
			 StationGroupDao gbean = iStation.fetchNextOutStation(dot.getDotno(), agv.getAgvId(), processList, true );
			if(gbean!=null) { 
				RoadDao road = iRoad.getTransRoad(475, gbean.getDotno()); 
				if(road!=null && road.getId()>0) {
					toQR(dot, agv, station, stationEvent,gbean.getProcess(), road.getId(),true);
				}else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", 475, gbean.getDotno())); 	
				}  
			}else {
				//返回空车,去476，走151
				toQR(dot, agv, station, stationEvent,"" , 151,false);
			} 
		}else {
			//带料，只能去背膜上料
			String process = "背膜上料" ; 
			StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), process,0,true );
			if(gbean!=null) { 
				RoadDao road = iRoad.getTransRoad(475, gbean.getDotno()); 
				if(road!=null && road.getId()>0) {
					toQR(dot, agv, station, stationEvent,gbean.getProcess(), road.getId(),true);
				}else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", dot.getDotno()-1, gbean.getDotno())); 	
				}  
			}else {
				//不动 
			} 
		}
	}

	/**
	 * 背膜缓存，去丝网
	 * @param dot
	 * @param agv
	 * @param station
	 * @param stationEvent
	 * @param amount
	 */
	void leaveBM(DotBean dot, AgvBean agv, StationBean station, IStationProtocol stationEvent, AgvMountBean amount) {
		if (amount.getMountType() == AgvMountTypeEnum.AllEmpty) {
			//空车，去392
			int roadid= iRoad.getNormalRoad(dot.getDotno()-1, 392);
			if(roadid>0) {
				toQR(dot, agv, station, stationEvent, "", roadid,false);
			}else {
				MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", dot.getDotno()-1, 392)); 
			} 
		}else {
			//带料，去451
			String process = "丝网" ; 
			StationGroupDao gbean = iStation.fetchNextInStation(dot.getDotno(), agv.getAgvId(), process,0,true );
			if(gbean!=null) { 
				int roadid= iRoad.getNormalRoad(dot.getDotno()-1, 451);
				if(roadid>0) {
					toQR(dot, agv, station, stationEvent,process, roadid,true);
				}else {
					MsgCore.sendMsg(agv.getAgvId(), String.format("%d与%d没有路线", dot.getDotno()-1, 451)); 
				}  
			}else {
				//不动 
			} 
		}
	}
}
